<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2016-2019 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w [y139w@163.com]
 * +--------------------------------------------------------------
 * | 2019/12/17 15:16
 * +--------------------------------------------------------------
 * | Log.php
 * +--------------------------------------------------------------
 */

namespace luo;


class Log
{

    /**
     * current Log class instance object
     * this is static property.
     * @var null
     */
    private static $_instance = null;

    /**
     * log file save path.
     * @var array|mixed|null
     */
    private $path;

    /**
     * Log constructor.
     */
    public function __construct()
    {
        $this->path = Config::get('log_file_path');
    }

    /**
     * use static method instance current class.
     * static property $_instance is log class instance object.
     * use static property $_instance.
     * @return Log|null
     */
    public static function instance(){

        if(!(self::$_instance instanceof self)){
            self::$_instance = new self();
        }
        return self::$_instance;

    }

    /**
     * write content to log file.
     * @param $content
     * @param int $line
     * @param bool $name
     * @param string $file
     * @return bool|int
     */
    public function write($content, $line = __LINE__, $name = false, $file = __FILE__){

        // detection directory exists
        $this->directoryExists($this->path);

        $dateTime = date('Y-m-d H:i:s w');
        $remote_addr = $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'];
        $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
        $request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
        $redirect_status = isset($_SERVER['REDIRECT_STATUS']) ? $_SERVER['REDIRECT_STATUS'] : '';
        $http_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        $http_accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';

        // splicing string contents to be written to log file.
        $string = '';
        $string .= "[{$dateTime}] {$remote_addr} {$request_method} {$request_uri} {$redirect_status}" . PHP_EOL;
        $string .= "output: ---- start ----" . PHP_EOL;
        $string .= "output: {$http_accept}" . PHP_EOL;
        $string .= "output: {$http_user_agent}" . PHP_EOL;
        $string .= "output: in {$file} on {$line} line" . PHP_EOL;
        $content = is_array($content) ? json_encode($content) : $content;
        $string .= "output: data>>> {$content} <<<data" . PHP_EOL;
        $string .= "output: ---- end ------" . PHP_EOL . PHP_EOL;

        // the name of the file to save the contents of the log.
        $filename = $this->path . 'log_';
        if($name){
            $filename = $this->path . $name . '_';
        }
        $filename .= date('Ymd') . '.log';

        return file_put_contents($filename, $string, FILE_APPEND);

    }

    /**
     * whether the directory exists, recursively create the directory if it does not exist.
     * @param $directory
     * @return bool
     */
    private function directoryExists($directory){

        if(!is_dir($directory)){
            return mkdir($directory, 0777, true);
        }
        return true;

    }

    /**
     * Using static methods to call ordinary methods in the current object.
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        // TODO: Implement __callStatic() method.
        $self = self::instance();
        if(method_exists($self, $name)){
            return call_user_func_array([$self, $name], $arguments);
        }

    }

}